package com.jvxb.demo.controller.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.jvxb.demo.utils.ResponseDataVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * <p>================================================
 * <p>Title: 基础 CURD 控制器
 * <p>Description: 通过继承本类，子类控制器可以直接使用增删改查接口。
 * <p>================================================
 *
 * @author jvxb
 * @version 1.0
 */
public abstract class BaseCURDController<T> {

    protected IService<T> commonService;

    @Autowired
    public void setCommonService(IService<T> commonService) {
        this.commonService = commonService;
    }

    @ApiOperation("新增")
    @PostMapping("/save")
    public ResponseDataVo<T> save(@RequestBody T t) {
        commonService.save(t);
        return ResponseDataVo.success(t);
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public ResponseDataVo<Void> update(@RequestBody T t) {
        commonService.updateById(t);
        return ResponseDataVo.success();
    }

    @ApiOperation("删除id")
    @PostMapping("/delete/{id}")
    public ResponseDataVo<Void> delete(@PathVariable("id") String id) {
        commonService.removeById(id);
        return ResponseDataVo.success();
    }

    @ApiOperation("查找id")
    @GetMapping("/get/{id}")
    public ResponseDataVo<T> get(@PathVariable("id") String id) {
        return ResponseDataVo.success(commonService.getOne(new QueryWrapper<T>().eq("id", id)));
    }

}
